package fast.mvc;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import javax.swing.SwingWorker;

import net.wsnetwork.reforger.Reforger;
import net.wsnetwork.reforger.ReforgingOption;
import net.wsnetwork.reforger.Stat;
import fast.loader.Reforge;
import fast.loader.Slot;

public class AutoReforgeTask extends SwingWorker<Void, Void> {
	
	Reforger ref = null;
	Model model = null;
	View view = null;
	int expReduction = 0;
	
	public AutoReforgeTask(Model m,View v,int expR){
		model = m;
		view = v;
		expReduction = expR;
	}
    @Override
    public Void doInBackground() {
    	try{
    		ref = new Reforger();
    		System.out.println("Reforge with spec: " + model.getLoader().getBase().getSpec());
    		System.out.println("expReduction: " + expReduction);
    		ref.runReforging3((HashMap<String, Double>) model.getEP(),model.getEquipment(),model.getLoader().getBase().getSpec(),expReduction);
    		System.out.println("Reforge done!");
    	} catch (InterruptedException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
        return null;
    }

    @Override
    public void done() {
		List<ReforgingOption> results = ref.getResults();
		if(results != null){				
			for (ReforgingOption option : results) {
				Reforge re = new Reforge();
				for(int p = 0;p < Stat.possibleStats.length;p++){
					for(fast.data.Stat mystat : fast.data.Stat.possibleReforgeStats ){
						if(!Stat.possibleStats[p].getReforgeName().equals(mystat.getReforgeName()))
							continue;
						if(Stat.possibleStats[p].getIndex() == option.getFromStatIndex()){
							re.setFrom(mystat);
						}else if(Stat.possibleStats[p].getIndex() == option.getToStatIndex()){
							re.setTo(mystat);
						}
					}
					
				}
				//re = reforgeDB.getReforge(re);
				Reforge curReforge = model.getEquipment().get(Slot.valueOf(option.getItem().getSlotName())).getReforge();
				if(curReforge == null){
					curReforge = new Reforge();
				}
				if(!curReforge.equals(re)){
					View.addLog("Reforging " + option.getItem().getName() + " to -" + option.getDeltaValue() + " " + re.getFrom() + " / +" + option.getDeltaValue() + " " + re.getTo());
				}
				model.getEquipment().setAutoReforge(Slot.valueOf(option.getItem().getSlotName()), re);
	        }
		}
		Set<net.wsnetwork.reforger.Item> notReforgedItems = ref.getNotReforgedItems();
		if(notReforgedItems != null){
			for(net.wsnetwork.reforger.Item item : notReforgedItems){
				Reforge curReforge = model.getEquipment().get(Slot.valueOf(item.getSlotName())).getReforge();
				if(curReforge != null){
					model.getEquipment().setAutoReforge(Slot.valueOf(item.getSlotName()), null);
					View.addLog("Removed reforge from " + item.getName());
				}
			}
		}
		model.getEquipment().fireAutoReforgeComplete();
		view.getAutoReforgeButton().setEnabled(true);
        view.getLoadingImage().setVisible(false);
    }
}
